use derive_builder::Builder;
use serde::{Deserialize, Serialize};

use crate::error::Error;
use crate::mapper::ValueMapper;
use crate::modeling::{
    Class, ClassAttributeBuilder, ClassBuilder, ClassGetter, ClassRelationBuilder,
    RelationRelatedType,
};
use crate::modeling::{
    ClassAttributePersistence, ClassPersistence, ClassPersistenceBuilder, ClassPersistenceGetter,
    ClassRelation, ClassRelationGetter, Value,
};

use super::ProjectApplication;

const PROJECT_APPLICATION_CONFIG_CLASS_IDENTIFIER: &'static str = "g01_project_application_config";

const PROJECT_APPLICATION_CONFIG_CLASS_NAME: &'static str = "project_application_config";

const PROJECT_APPLICATION_CONFIG_TABLE_NAME: &'static str = "_g01_meta_project_application_config";

#[derive(Debug, Serialize, Deserialize, Default, Clone, Builder, PartialEq)]
#[builder(name = "ProjectApplicationConfigBuilder")]
#[builder(pattern = "mutable")]
#[builder(setter(into, strip_option), default)]
#[builder(derive(Debug))]
#[builder(build_fn(error = "Error"))]
pub struct ProjectApplicationConfig {
    /// id
    pub id: i64,
    /// project_identifier
    pub application_identifier: String,
    /// identifier
    pub identifier: String,
    /// 名称
    pub name: String,
    /// 名称
    pub title: String,
    /// 图标
    pub icon: Option<String>,
    /// 选项
    pub options: Option<String>,
    /// 备注
    pub description: String,
    /// 创建时间
    pub created_at: i64,
    /// 更新时间
    pub updated_at: i64,
}

impl ValueMapper for ProjectApplicationConfig {}

//
crate::impl_from_value!(ProjectApplicationConfig);
crate::impl_class_persistence_getter!(
    ProjectApplicationConfig,
    PROJECT_APPLICATION_CONFIG_TABLE_NAME
);
crate::impl_class_relations_getter!(
    ProjectApplicationConfig,
    vec![ClassRelationBuilder::default()
        .src_identifier(ProjectApplication::get_identifier()?)
        .target_identifier(Self::get_identifier()?)
        .related_type(RelationRelatedType::Keys)
        .related_at("application_identifier")
        .build()?,]
);

impl ClassGetter for ProjectApplicationConfig {
    fn get_name() -> anyhow::Result<String> {
        Ok(String::from(PROJECT_APPLICATION_CONFIG_CLASS_NAME))
    }
    fn get_identifier() -> anyhow::Result<String> {
        Ok(String::from(PROJECT_APPLICATION_CONFIG_CLASS_IDENTIFIER))
    }
    fn get_class() -> anyhow::Result<Class> {
        let mut attribute_builder = ClassAttributeBuilder::default();
        attribute_builder.class_identifier(PROJECT_APPLICATION_CONFIG_CLASS_IDENTIFIER);

        let mut commons = crate::modeling::common_attributes_with_builder(&attribute_builder)?;
        let mut append = vec![];
        append.append(&mut commons);
        append.sort_by(|a, b| a.position.cmp(&b.position));

        let clazz = ClassBuilder::default()
            .identifier(PROJECT_APPLICATION_CONFIG_CLASS_IDENTIFIER)
            .name(PROJECT_APPLICATION_CONFIG_CLASS_NAME)
            .data_is_type(true)
            .is_persistent(true)
            .primary_key(String::from("identifier"))
            .attributes(append)
            .build()?;
        Ok(clazz)
    }
}
